package com.poetize.plus.entity.dto;

import com.poetize.plus.entity.po.Menu;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;

/**
 * @author zuosy
 * @created 2024/3/29 14:49
 */
public record MenuDTO(
        @Schema(description = "id")
        @Null(message = "id必须为空", groups = {Insert.class})
        @NotNull(message = "id不能为空", groups = {Update.class})
        Long id,

        @Schema(description = "父菜单id")
        @NotNull(message = "父菜单id不能为空", groups = {Insert.class, Update.class})
        Long parentId,

        @Schema(description = "菜单分区（0：后端，1：门户）")
        @NotBlank(message = "菜单分区不能为空", groups = {Insert.class, Update.class})
        String partition,

        @Schema(description = "菜单名称")
        @NotBlank(message = "菜单名称不能为空", groups = {Insert.class, Update.class})
        String name,

        @Schema(description = "排序")
        @NotNull(message = "排序不能为空", groups = {Insert.class, Update.class})
        @Min(value = 0, message = "排序不能低于0", groups = {Insert.class, Update.class})
        Integer sort,

        @Schema(description = "路由地址")
        @NotBlank(message = "路由地址不能为空", groups = {Insert.class, Update.class})
        String path,

        @Schema(description = "菜单图标")
        @NotBlank(message = "菜单图标不能为空")
        String icon,

        @Schema(description = "组件路径")
        @NotBlank(message = "组件路径不能为空")
        String component,

        @Schema(description = "组件名")
        @NotBlank(message = "组件名不能为空")
        String componentName,

        @Schema(description = "是否禁用")
        @NotNull(message = "是否禁用不能为空", groups = {Insert.class, Update.class})
        Boolean visible
) {
    /**
     * DTO转实体类
     *
     * @param menuDTO
     * @return
     */
    public static Menu convertDTO(MenuDTO menuDTO) {
        return new Menu(
                menuDTO.id,
                menuDTO.parentId,
                menuDTO.partition,
                menuDTO.name,
                menuDTO.sort,
                menuDTO.path,
                menuDTO.icon,
                menuDTO.component,
                menuDTO.componentName,
                menuDTO.visible);
    }
}
